﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Phoenix.Service.Contracts;
using Phoenix.Service;
using Phoenix.Service.Model;
using Phoenix.Enum;

namespace Phoenix.AdminSite.Controllers
{
    public class OrganizationController : ControllerBase
    {
        IOrganizationService IOrganization = new OrganizationService();
        IRightsTypeService IRightsType = new RightsTypeService();
        IIDCreaterService IIDCreater = new IDCreaterService();

        public ActionResult Index()
        {
            ViewData["OrganizationList"] = IOrganization.GetPath();

            return View();
        }

        public ActionResult DepartRight()
        {
            ViewData["RightsAndType"] = IRightsType.GetRightsAndType();

            return View();
        }

        public ActionResult Edit()
        {

            int action = int.Parse(Request.QueryString["action"]);
            int orgID = int.Parse(Request.QueryString["orgID"]);

            OrganizationModel model  = IOrganization.GetByOrgID(orgID);
            string namePath = model.NamePath;
            
            if (action == 1)
            {
                model = null;
            }
            ViewData["OrgID"] = orgID;
            ViewData["NamePath"] = namePath;
            ViewData["OrganizationModel"] = model;
            ViewData["Action"] = action;


            return View();
        }

        public ActionResult Save(FormCollection collection, string hdaction)
        {

            int orgID = int.Parse(collection["hdOrgID"]);

            OrganizationModel model = IOrganization.GetByOrgID(orgID);

            model.Address = collection["txtAddress"];
            model.LeaderID = int.Parse(collection["txtLeaderID"]);
            model.LeaderName = collection["txtLeaderName"];
            model.OfficePhone = collection["txtOfficePhone"];
            model.OrgName = collection["txtOrgName"];

            if ((ActionType)int.Parse(hdaction) == ActionType.Add)
            {
                model.OrgID = IIDCreater.GetNewID("OrgID", "部门编号", 1000);
                model.CreateTime = DateTime.Now;
                model.CreateUser = 1;
                model.DeleteFlag = false;
                model.OrgType = 1;
                model.ParentID = orgID;
                model.IDPath = model.IDPath + "-" + model.OrgID;
                model.NamePath = model.NamePath + "-" + model.OrgName;
                IOrganization.AddOrganization(model);
                
            }
            else
            {
                model.UpdateTime = DateTime.Now;
                IOrganization.UpdateByOrgID(model);
            }


            return ShowMsg("成功", Common.ActionStatus.OK, "");
        }

        public ActionResult Test()
        {
            ViewData["OrganizationList"] = IOrganization.GetPath();
            return View();
        }

    }
}
